package contactbook.dao.impl;

import java.util.HashMap;
import java.util.Set;

import contactbook.dao.ContactDAOInterface;
import contactbook.model.Contact;
import contactbook.model.Group;

public class ContactDAO implements ContactDAOInterface {
	
	private static int cmpt = 0;
	
	private HashMap<Integer, Contact> contacts;
	private HashMap<Integer, Integer> contactsOfGroups;
	
	public ContactDAO() {
		contacts = new HashMap<Integer, Contact>();
		contactsOfGroups = new HashMap<Integer, Integer>();
	}
	
	@Override
	public void affectGroup(int contactId, int groupId) {
		contactsOfGroups.put(contactId, groupId);
	}

	@Override
	public Contact addContact(Contact contact, int groupId) {
		contact.setId(getNextId());
		contacts.put(contact.getId(), contact);
		contactsOfGroups.put(contact.getId(), groupId);
		return contact;
	}

	@Override
	public void delete(int contactId) {
		contactsOfGroups.remove(contactId);
		contacts.remove(contactId);
	}

	@Override
	public Contact modify(int contactId, Contact temp) {
		contacts.get(contactId).setAddress(temp.getAddress());
		contacts.get(contactId).setAge(temp.getAge());
		contacts.get(contactId).setCountry(temp.getCountry());
		contacts.get(contactId).setEmail(temp.getEmail());
		contacts.get(contactId).setFirstname(temp.getFirstName());
		contacts.get(contactId).setLastName(temp.getLastName());
		contacts.get(contactId).setMaritalstatus(temp.getMaritalstatus());
		contacts.get(contactId).setTown(temp.getTown());
		contacts.get(contactId).setZipCode(temp.getZipCode());
		return contacts.get(contactId);
	}

	@Override
	public int getGroupId(int contactId) {
		return contactsOfGroups.get(contactId);
	}

	@Override
	public Contact getContactById(int contactId) {
		return contacts.get(contactId);
	}
	
	public boolean exists(int contactId) {
		return contacts.containsKey(contactId);
	}
	
	public boolean exists(Contact c) {
		return contacts.containsValue(c);
	}
	
	private static int getNextId() {
		return ++cmpt;
	}
}
